package org.speedy.analysis;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TypeExtractor {

	private static final Pattern CLSID = Pattern.compile("\\[*?L(.*?(/.*?)*);");

	public String processType(String desc) {
		Matcher m = CLSID.matcher(desc);
        if (m.matches()) {
            if (m.group(1).length() == 1) return null;
            return m.group(1);
        }
        return null;
	}

	public String[] processMethod(String desc) {
		String returnTypeDesc = desc.substring(desc.lastIndexOf(")") + 1);
        String[] params;
        if (desc.startsWith("()"))
            params = new String[] {};
        else {
            String paramString = desc.substring(1, desc.lastIndexOf(")"));
            params = paramString.split(";");
        }

        String[] result = new String[params.length + 1];
        result[0] = this.processType(returnTypeDesc);
        int index = 1;
        for (String c : params) {
            if (!c.contains("L")) continue;
            result[index++] = this.processType(c.substring(c.indexOf("L")) + ";");
        }
        return result;
	}
}